from five import grok
from zope import schema
from plone.directives import form

from zope.interface import Invalid

from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

from plone.namedfile.interfaces import IImageScaleTraversable
from plone.namedfile.field import NamedBlobImage

from plone.app.textfield import RichText

from optilux.cinemacontent import CinemaMessageFactory as _

# Indexer
from plone.indexer import indexer

# Uniqueness validator
from z3c.form import validator
from plone.uuid.interfaces import IUUID
from Products.CMFCore.utils import getToolByName

class IReservation(form.Schema):
    """Describes a reservation for one person over a determined period
    """

    startDate = schema.Date(
            title=_(u'Start date'),
            description=_(u"First date that the film is showing"),
        )
        
    endDate = schema.Date(
            title=_(u'End date'),
            description=_(u"Last date that the film is showing"),
            required=True
        )

    birthdate = schema.Date(
            title=_(u"Birthdate"),
            description=_(u"Please enter the person's date of birth"),
            required=True
        )


    gender = schema.Choice(
            title=_(u"Gender"),
            values=_(u"Male", u"Female",)
        )


@grok.adapter(IReservation, name='start')
@indexer(IReservation)
def reservationStartIndexer(context):
    """Create a catalogue indexer, registered as an adapter, which can
    populate the ``start`` index with the reservation's start date.
    """
    return context.startDate

@grok.adapter(IReservation, name='end')
@indexer(IReservation)
def reservationEndIndexer(context):
    """Create a catalogue indexer, registered as an adapter, which can
    populate the ``end`` index with the reservation's end date.
    """
    return context.endDate

class View(grok.View):
    """Default view (called "@@view"") for a reservation.
    
    The associated template is found in reservation_templates/view.pt.
    """
    
    grok.context(IReservation)
    grok.require('zope2.View')
    grok.name('view')
    
    def update(self):
        """Prepare information for the template
        """
        
        self.startDateFormatted = self.context.startDate.strftime("%d %b %Y")
        self.endDateFormatted = self.context.endDate.strftime("%d %b %Y")
